package com.sesame.sysuser.controller;

import com.sesame.sysuser.bean.SysUser;
import com.sesame.sysuser.dao.SysUserDao;
import com.sesame.sysuser.service.SysUserService;
import kim.sesame.common.result.ApiResult;
import kim.sesame.common.web.controller.AbstractWebController;
import lombok.extern.apachecommons.CommonsLog;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * SysUserController
 *
 * @author admin
 * @date 2019-02-16 10:22:58
 * @Description: 系统用户, 测试mybatis-plus
 */
@CommonsLog
@RestController
@RequestMapping("/sysuser")
public class SysUserController extends AbstractWebController {

    @Autowired
    private SysUserService sysUserService;

    @SuppressWarnings("all")
    @Autowired
    private SysUserDao sysUserDao;

    /**
     * 查询list
     *
     * @author admin
     * @date 2019-02-16 10:22:58
     * @Description:
     */
    @RequestMapping("/list")
    public ApiResult list(SysUser bean) {

        List<SysUser> list = sysUserService.searchList(bean);

        return success(list);
    }

    /**
     * 分页查询
     *
     * @author admin
     * @date 2019-02-16 10:22:58
     * @ pageNum : 页码,默认1
     * @ pageSize :  页面大小
     */
//    @RequestMapping("/listPage")
//    public ApiResult listPage(SysUser bean, GPage gPage) {
//
////        Page<SysUser> pages = PageHelper.startPage(gPage.getPageNum(), gPage.getPageSize())
////                .doSelectPage(() -> sysUserService.searchList(bean));
////        List<SysUser> list = pages.getResult();
//
////        return success(list, PageUtil.recount(gPage, pages));
//        return success();
//    }

    /**
     * mybatis plus  分页
     *
     * @param bean
     * @param gPage
     * @return
     */
//    @RequestMapping("/listPage2")
//    public ApiResult listPage2(SysUser bean, GPage gPage) {
//
//        List<SysUser> list = sysUserService.searchList2(gPage, bean);
//
//        return success(list, gPage);
//    }

    /**
     * 自定义查询列
     *
     * @param bean
     * http://127.0.0.1:8055/sysuser/list3
     */
    @RequestMapping("/list3")
    public ApiResult list3(SysUser bean) {

        List<SysUser> list = sysUserService.searchList3(bean);

        return success(list);
    }
    /**
     * 自定义查询列,返回map
     *
     * @param bean
     * http://127.0.0.1:8055/sysuser/list3
     */
    @RequestMapping("/list4")
    public ApiResult list4(SysUser bean) {

        List<Map<String, Object>> list = sysUserService.searchList4(bean);

        return success(list);
    }

    /**
     * 保存
     *
     * @author admin
     * @date 2019-02-16 10:22:58
     * @Description:
     */
    @RequestMapping("/save")
    public ApiResult save(SysUser bean) {
        //IUser user = UserContext.getUserContext().getUser();
        int res = 0;
        if (StringUtils.isNotEmpty(bean.getId())) {
            // 主键不为空, 修改数据
            bean.initUpdate("0000");
            res = sysUserService.update(bean);
        } else {
            //主键为空, 新增数据
            bean.initCreateAndId("0000");
            res = sysUserService.add(bean);
        }
        return success(res);
    }

    /**
     * 删除
     *
     * @author admin
     * @date 2019-02-16 10:22:58
     * @Description:
     */
    @RequestMapping("/delete")
    public ApiResult delete(String ids, HttpServletRequest request) {
        int res = sysUserService.delete(ids);
        return success(res);
    }

    /**
     * 查询详情
     *
     * @author admin
     * @date 2019-02-16 10:22:58
     * @Description:
     */
    @RequestMapping("/searchDetail")
    public ApiResult searchDetail(SysUser bean, HttpServletRequest request) {
        bean = sysUserService.search(bean);
        return success(bean);
    }

    @RequestMapping("/aaa")
    public ApiResult aaa() {
        List<SysUser> list = sysUserService.aaa();
        return success(list);
    }

}
